package galaxies;

import eventManager.EventManager;
import galaxies.enums.SpiralArmNumbers;
import gameAppLayer.GameSettings;
import gameAppLayer.enums.Constants;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Random;
import java.util.logging.Level;
import java.util.logging.Logger;

import testClasses.misc.SpiralGalaxyTest;
import utilities.HexLocationLoader;
import utilities.HexTileGen;
import utilities.MVCMaker;
import view.EViewList;
import view.AbstractView;

import com.jme3.asset.AssetManager;

import controller.AbstractController;
import controller.IController;

public class GalaxySpiralBuilder2 {
	
	private static final Logger logger = Logger.getLogger(GalaxySpiralBuilder2.class.getName());
	
	private HashMap<Integer, ArrayList<Integer>> galaxyTileLocations;
	private int numArms;	
	
	public ArrayList<IController> createGalaxy(
			GameSettings settings,			
			AssetManager assetManager, 
			String galRef, 
			ArrayList<ArrayList<Integer>> dims, 
			SpiralArmNumbers armNumbers) {///
		
		logger.log(Level.INFO, "CREATING GALAXY");
		GalaxyModel model = new GalaxyModel(settings.getGalSizes(galRef), settings.getGalaxyType(galRef), settings.getGalaxyDensity(galRef));
		GalaxyViewBuilder viewBuild = new GalaxyViewBuilder();
		HashMap<EViewList, AbstractView> view = viewBuild.buildViews(assetManager);
		GalaxyController controller = new GalaxyController();
		
		MVCMaker.linkElements(model, view, controller);
		
		ArrayList<IController> controllers = new ArrayList<IController>();
		controllers.add(controller);
		logger.log(Level.INFO, "GALAXY CREATED");
		
		model.setDims(dims);		
		logger.log(Level.INFO, "LOAD LOCATIONS START");
		
		int largestDim = (int) findLargest(dims);
		logger.log(Level.INFO, "Largest Dim= " + largestDim);
		//ArrayList<ArrayList<Integer>> locations = HexLocationLoader.loadLocations(dims.get(0).get(0));
		galaxyTileLocations = HexLocationLoader.loadLocations(largestDim);
		logger.log(Level.INFO, "LOAD LOCATIONS FINISH");
		model.setLocations(galaxyTileLocations);
		logger.log(Level.INFO, "LOCATIONS ADDED TO GALAXY");		
		
		//Randomly generate number of arms
		int galSize = settings.getGalSizes(galRef);
		numArms = generateNumArms(galSize, armNumbers);
		
		return controllers;
	}
	
	public double findLargest(ArrayList<ArrayList<Integer>> dims) {
		double dist;
		double maxDist = 0;		
		int x, y;
		for (ArrayList<Integer> point: dims) {
			x = point.get(0);
			y = point.get(1);
			
			dist = Math.sqrt(x*x + y*y);
			if (dist > maxDist) {
				maxDist = dist;				
			}			
		}
		
		return maxDist;
	}
	
	public HashMap<Integer, ArrayList<Integer>> getLocations() {
		return galaxyTileLocations;
	}
	
	public int getNumArms() {
		return this.numArms;
	}
	
	private int generateNumArms(int galSize, SpiralArmNumbers armNumbers) {
		Random rand = new Random();
		
		ArrayList<String> entries = armNumbers.getEntries();
		HashMap<String, Integer> minTiles = armNumbers.getSpiralArmNumbers();
		
		int maxArms = 0;
		for (String entry: entries) {			
			if (galSize >= minTiles.get(entry)) {
				maxArms = Integer.parseInt(entry);
			}
		}
		
		int numArms = 0;
		
		if (maxArms == 2)
		{numArms = 3;} //normal spirals have at least 3 arms
		else if (maxArms == 3)
		{numArms = 3;} //(int) Math.floor(rand.nextDouble()*2+2);}  //3
		else if (maxArms == 4)
		{numArms = (int) Math.floor(rand.nextDouble()*2+3);}  //3, or 4
		else if (maxArms == 6)
		{
			double coinFlip = rand.nextDouble();
			if (coinFlip < 0.33)
			{numArms = 3;}
			else if ((coinFlip >=0.33) && (coinFlip < 0.66))
			{numArms = 4;}
			else if ((coinFlip >= 0.66))
			{numArms = 6;}
		}
		
		return numArms;
	}

}
